﻿using MTQ.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MTQ.BLL
{
    public partial class Base_MenuService : BaseService<Base_Menu>
    {
        /// <summary>
        /// 获取左侧菜单
        /// </summary>
        /// <param name="MenuGuid"></param>
        /// <param name="listAuthMenus"></param>
        /// <returns></returns>
        public List<MTQ.Model.Base_Menu> InitSidebarMenu(Guid? MenuGuid, List<Guid> listAuthMenus)
        {
            var menuList = new Base_MenuService().LoadEntities(x => true).ToList();

            List<Base_Menu> tempMenu = new List<Base_Menu>();

            //查找第一级目录
            foreach (Base_Menu r in menuList.Where(x => x.Menu_ParentId == MenuGuid && x.Menu_Status == 1).OrderBy(x => x.Menu_Sort))
            {
                Base_Menu menuViewModel = r;

                //递归查找下一级
                menuViewModel.Items = InitSidebarMenu(menuViewModel.Menu_Id, listAuthMenus);

                if (r.Menu_Code != "00")
                {
                    if (listAuthMenus != null && !listAuthMenus.Contains(menuViewModel.Menu_Id))
                    {
                        continue;
                    }
                }

                tempMenu.Add(menuViewModel);
            }

            return tempMenu;
        }

        /// <summary>
        /// 获取所有菜单
        /// </summary>
        /// <param name="MenuGuid"></param>
        /// <returns></returns>
        public List<MTQ.Model.Base_Menu> MenuAll(Guid? MenuGuid)
        {
            var menuList = new Base_MenuService().LoadEntities(x => true).ToList();

            List<Base_Menu> tempMenu = new List<Base_Menu>();

            //查找第一级目录
            foreach (Base_Menu r in menuList.Where(x => x.Menu_ParentId == MenuGuid && x.Menu_Status == 1).OrderBy(x => x.Menu_Sort))
            {
                Base_Menu menuViewModel = r;

                //递归查找下一级
                menuViewModel.Items = MenuAll(menuViewModel.Menu_Id);

                tempMenu.Add(menuViewModel);
            }

            return tempMenu;
        }

        /// <summary>
        /// 获取所有菜单  除去主页 menu_type=1
        /// </summary>
        /// <param name="MenuGuid"></param>
        /// <param name="menu_type"></param>
        /// <returns></returns>
        public List<MTQ.Model.Base_Menu> MenuAll(Guid? MenuGuid, int menu_type)
        {
            var menuList = new Base_MenuService().LoadEntities(x => x.Menu_Type == menu_type).ToList();

            List<Base_Menu> tempMenu = new List<Base_Menu>();

            //查找第一级目录
            foreach (Base_Menu r in menuList.Where(x => x.Menu_ParentId == MenuGuid && x.Menu_Status == 1).OrderBy(x => x.Menu_Sort))
            {
                Base_Menu menuViewModel = r;

                //递归查找下一级
                menuViewModel.Items = MenuAll(menuViewModel.Menu_Id, menu_type);

                tempMenu.Add(menuViewModel);
            }

            return tempMenu;
        }


        /// <summary>
        /// 获取菜单id  
        /// </summary>
        /// <param name="menuList"></param>
        /// <param name="listguid"></param>
        /// <param name="listresult"></param>
        public static void MenuIDs(List<Base_Menu> menuList, List<Guid?> listguid, List<Guid> listresult)
        {
            //var menuList = new Base_MenuService().LoadEntities(x => true).ToList();
            foreach (var item in listguid)
            {
                Base_Menu menu = menuList.Where(x => x.Menu_Id == item).FirstOrDefault();
                if (menu != null)
                {
                    listresult.Add(menu.Menu_Id);
                    if (menu.Menu_ParentId != null)
                    {
                        MenuIDs(menuList, new List<Guid?>() { menu.Menu_ParentId }, listresult);
                    }
                }
            }
        }


        /// <summary>
        /// 获取用户菜单
        /// </summary>
        /// <param name="MenuGuid"></param>
        /// <param name="listmenuids"></param>
        /// <returns></returns>
        public List<LayuiMenuInfo> UserMenu(Guid? MenuGuid,List<Guid> listmenuids,string CultureInfoName) 
        {
            var menuList = new Base_MenuService().LoadEntities(x => x.Menu_Type == 1).ToList();

            List<LayuiMenuInfo> tempMenu = new List<LayuiMenuInfo>();
            foreach (Base_Menu r in menuList.Where(x => x.Menu_ParentId == MenuGuid && x.Menu_Status == 1).OrderBy(x => x.Menu_Sort))
            {
                LayuiMenuInfo menuViewModel = new LayuiMenuInfo() { href = r.Menu_NavigateUrl, target = r.Menu_Target, icon = r.Menu_Img, image = r.Menu_Img };

                menuViewModel.title = r.Menu_Title;//Base_LocalService.GetWord(r.Menu_Title, CultureInfoName);
                if (!listmenuids.Contains(r.Menu_Id))
                {
                    continue;
                }
                //递归查找下一级
                menuViewModel.child = UserMenu(r.Menu_Id, listmenuids, CultureInfoName);

                tempMenu.Add(menuViewModel);
            }


            return tempMenu;
        }

    }
}
